package com.yuliang.ai.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuliang.ai.domain.Dto.PageDto;
import com.yuliang.ai.domain.Vo.UserOperationRecordVo;
import com.yuliang.ai.domain.entity.UserOperationRecord;
import com.yuliang.ai.mapper.UserOperationRecordMapper;
import com.yuliang.ai.service.UserOperationRecordService;
import com.yuliang.utils.result.ResultUtils;
import com.yuliang.utils.result.emums.HttpCodeEnum;
import com.yuliang.utils.superbackage.SuperServiceImpl;
import com.yuliang.utils.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Huangkai
 */
@Slf4j
@Service

public class UserOperationRecordServiceImpl extends SuperServiceImpl<UserOperationRecordMapper, UserOperationRecord> implements UserOperationRecordService {

    @Autowired
    UserOperationRecordMapper userOperationRecordMapper;

    @Override
    public PageDto UserOperationRecordList(UserOperationRecordVo userOperationRecordVo) {
        PageDto pageDto = new PageDto();
        LambdaQueryWrapper<UserOperationRecord> wrapper = new QueryWrapper<UserOperationRecord>().lambda();
        wrapper.eq(UserOperationRecord::getUserId, userOperationRecordVo.getUserId());
        wrapper.orderByDesc(UserOperationRecord::getCreateTime);
        wrapper.orderByDesc(UserOperationRecord::getId);
        //分页获取
        Page<UserOperationRecord> page = new Page<>();
        page.setCurrent(userOperationRecordVo.getPage());
        page.setSize(userOperationRecordVo.getLimit());
        IPage<UserOperationRecord> humanCollectionIPage = userOperationRecordMapper.selectPage(page, wrapper);
        pageDto.setTotal(humanCollectionIPage.getTotal());
        pageDto.setRecords(humanCollectionIPage.getRecords());
        pageDto.setSize(humanCollectionIPage.getSize());
        pageDto.setCurrent(humanCollectionIPage.getCurrent());
        return pageDto;
    }

    @Override
    public String insertUserOperationRecord(UserOperationRecordVo userOperationRecordVo) {
        UserOperationRecord userOperationRecord = new UserOperationRecord();
        userOperationRecord.setUuid(StringUtils.getSnowFlakeId());
        userOperationRecord.setUserId(userOperationRecordVo.getUserId());
        userOperationRecord.setChartId(userOperationRecordVo.getChartId());
        userOperationRecord.setChartAndId(userOperationRecordVo.getChartAndId());
        userOperationRecord.setHumanIdentifyId(userOperationRecordVo.getHumanIdentifyId());
        userOperationRecord.insert();
        return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.insert_success);
    }
}
